<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.wr_id ? '编辑' : '添加'" v-model="visible" append-to-body width="600px"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <el-form-item label="平台">
        <el-radio-group v-model="form.platform_id" @change="form.site_id = []" :disabled="!!form.wr_id">
          <el-radio v-for="(d, i) in options.platform" :key="i" :label="d.value">{{ d.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="站点">
        <VSelect v-model="form.site_id" placeholder="请选择" :options="siteList" class="w-full"
          @change="({ option }) => (form.site_name = option.label)" :disabled="!!form.wr_id" />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouse" :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
        <VSelect v-model="form.warehouse" placeholder="请选择" type="warehouse" multiple collapse-tags class="w-full"
          @change="({ labels }) => (form.warehouse_name = labels)" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入"
          :maxlength="800" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import api2 from '../../prohibited/api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()
const siteList = ref([])

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (wr_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true
  if (wr_id) {
    const res = await api.detail({ wr_id })
    form.value = res.data
    formRef?.value?.clearValidate()
  } else {
    form.value = { platform_id: props.options.platform[0].value, status: 1 }
    formRef?.value?.resetFields()
  }
}

const getSiteList = async (v) => {
  const res = await api2.getPlatform({platform_id: v})
  siteList.value = res.data.map(v => ({label: v.site_name, value: v.site_id}))
}

watch(() => form.value.platform_id, value => {
  if (value) {
    getSiteList(value)
  }
})


// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api[form.value.wr_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>